package com.example.hangzhou.welbeing.mytestdemoapp.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

/**
 * 杭州惟君医疗科技有限公司11
 * Created by 20433 on 2018/5/4
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public abstract class BasePageFragment extends Fragment {
    protected boolean isViewInitiated;
    protected boolean isVisibleToUser;
    protected boolean isDataInitiated;

    public Context mcontext;
    public Handler mhandler;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
        mhandler = new Handler();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

    public boolean  prepareFetchData() {
        return prepareFetchData(true);
    }

    public boolean prepareFetchData(boolean forceUpdate) {

        if(isViewInitiated && isVisibleToUser && (!isDataInitiated&& forceUpdate)){
            fetchData();
            isDataInitiated = true;
            return true;
        }
        isDataInitiated = false;
        return false;
    }

    protected abstract void fetchData();
}
